﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LibraryO2S;
using System.IO;
using BusinessObjects;

public partial class controls_admin_advFooter : Pages_Base
{
    protected static int i = 0;
    private static int index = -1;
    private static DataSet dsAdv;
    protected static string oldImage = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        i = 0;
        if (divMessage.InnerHtml != "")
            divMessage.InnerHtml = "";
        if (!IsPostBack)
        {
            btnUpdate.Enabled = false;
            loadAdv();
        }
        if (index == -1)
        {

        }
    }
    private void loadAdv()
    {
        try
        {
            base.OpenConnection();
            dsAdv = new DataSet("Home");
            try
            {
                dsAdv.ReadXml(Server.MapPath("../upload/xml/Home.xml"));
            }
            catch { }
            DataTable objDT;
            if (dsAdv.Tables.Count > 0)
                objDT = dsAdv.Tables[0];
            else
            {
                objDT = new DataTable("Home");
                objDT.Columns.Add("Title", typeof(String));
                objDT.Columns.Add("Desc", typeof(String));
                objDT.Columns.Add("Picture", typeof(String));
                objDT.Columns.Add("LinkTitle1", typeof(String));
                objDT.Columns.Add("Link1", typeof(String));
                objDT.Columns.Add("LinkTitle2", typeof(String));
                objDT.Columns.Add("Link2", typeof(String));
                dsAdv.Tables.Add(objDT);
            }
            if (objDT.Rows.Count > 0)
            {
                rptAdvertise.DataSource = objDT;
                rptAdvertise.DataBind();
            }
            else
            {
                rptAdvertise.DataSource = null;
                rptAdvertise.DataBind();
                divMessage.InnerHtml = O2S_Message.Warning("Thông Báo", "Chưa có bản ghi nào !");
            }
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi :<br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            base.OpenConnection();
            string title = txtTitle.Text.Trim();
            string desc = txtDesc.Text.Trim();
            string titlelink1 = txtTitle1.Text.Trim();
            string link1 = txtLink1.Text.Trim();
            string titlelink2 = txtTitle2.Text.Trim();
            string link2 = txtLink2.Text.Trim();
            if (fuAdv.HasFile)
            {
                oldImage = fuAdv.PostedFile.FileName;
                int count = 0;
                while (File.Exists(Server.MapPath("~/upload/home/") + oldImage))
                {
                    oldImage = count + "_" + fuAdv.FileName;
                    count++;
                }
                fuAdv.SaveAs(Server.MapPath("../upload/home/" + oldImage));
            }
            else
            {
                if (oldImage.Equals(""))
                {
                    divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phải chọn ảnh !");
                    return;
                }
            }
            DataRow drAdv;
            if (index == -1)
                drAdv = dsAdv.Tables[0].NewRow();
            else
                drAdv = dsAdv.Tables[0].Rows[index];
            drAdv["Title"] = title;
            drAdv["Desc"] = desc;
            drAdv["Picture"] = oldImage;
            drAdv["LinkTitle1"] = titlelink1;
            drAdv["Link1"] = link1;
            drAdv["LinkTitle2"] = titlelink2;
            drAdv["Link2"] = link2;
            if (index == -1)
                dsAdv.Tables[0].Rows.InsertAt(drAdv, 0);
            dsAdv.WriteXml(Server.MapPath("../upload/xml/Home.xml"));
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công.");
            //index = -1;
            loadAdv();
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi :<br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    protected void rptAdvertise_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            string flag = e.CommandName;
            index = e.Item.ItemIndex;
            if (flag.Equals("edit"))
            {
                DataRow drAdv = dsAdv.Tables[0].Rows[index];
                txtTitle.Text = drAdv["Title"].ToString();
                txtDesc.Text = drAdv["Desc"].ToString();
                txtTitle1.Text = drAdv["LinkTitle1"].ToString();
                txtLink1.Text = drAdv["Link1"].ToString();
                txtTitle2.Text = drAdv["LinkTitle2"].ToString();
                txtLink2.Text = drAdv["Link2"].ToString();
                oldImage = drAdv["picture"].ToString();
                btnUpdate.Enabled = true;

            }
            else
            {
                //Response.Write(flag);
                dsAdv.Tables[0].Rows.RemoveAt(index);
                dsAdv.WriteXml(Server.MapPath("../upload/xml/Home.xml"));
                loadAdv();
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Đã xóa thành công.");
            }

        }
        catch (Exception ex)
        {
            throw;
            //Response.Write(ex.StackTrace);
        }
    }
}
